let logger = require("pomelo-logger").getLogger(__filename);
const HandlerUtil = require("../../../utils/handlerUtil");

module.exports = function (app) {
    return new Handler(app);
};

let Handler = function (app) {
    this.app = app;
};

Handler.prototype.getYuanBaoTables = function (msg, session, callback) {
    let service = this.app.get("zhajinhuaTableService");
    callback(null, HandlerUtil.getYuanBaoTables(service, msg.rid));
};

Handler.prototype.createYuanBaoTable = function (msg, session, callback) {
    let service = this.app.get("zhajinhuaTableService");
    HandlerUtil.createYuanBaoTable(this.app, service, msg, session, callback, function (msg) {
        let data = {};
        data["maxPlayers"] = 5;
        data["maxRounds"] = [5, 5, 10, 15][msg.maxRounds];
        data["minSeeCardRound"] = [0, 0, 1, 3][msg.minSeeCardRound];
        data["minCompareRound"] = [1, 1, 2][msg.minCompareRound];
        data["compareMultiplier"] = [2, 2, 1][msg.compareMultiplier];
        data["visible"] =  msg.isVisible === 1 ? 1 : 0;
        return data;
    });
};

Handler.prototype.leaveTable = function (msg, session, callback) {
    let service = this.app.get("zhajinhuaTableService");
    HandlerUtil.leaveTable(service, this.app, session, callback);
};

Handler.prototype.getReady = function (msg, session, callback) {
    let zhajinhuaTableService = this.app.get("zhajinhuaTableService");
    callback(null, zhajinhuaTableService.getReady(session.frontendId, session.id));
};

Handler.prototype.joinYuanBaoTable = function (msg, session, callback) {
    let service = this.app.get("zhajinhuaTableService");
    HandlerUtil.joinYuanBaoTable(this.app, service, msg, session, callback);
};

Handler.prototype.quickJoinYuanBaoTable = function (msg, session, callback) {
    let service = this.app.get("zhajinhuaTableService");
    HandlerUtil.quickJoinYuanBaoTable(this.app, service, msg, session, callback);
};

Handler.prototype.seeCard = function (msg, session, callback) {
    let zhajinhuaTableService = this.app.get("zhajinhuaTableService");
    callback(null, zhajinhuaTableService.seeCard(session.frontendId, session.id));
};

Handler.prototype.fold = function (msg, session, callback) {
    let zhajinhuaTableService = this.app.get("zhajinhuaTableService");
    callback(null, zhajinhuaTableService.fold(session.frontendId, session.id));
};

Handler.prototype.bet = function (msg, session, callback) {
    let zhajinhuaTableService = this.app.get("zhajinhuaTableService");
    callback(null, zhajinhuaTableService.bet(session.frontendId, session.id, msg.amount));
};

Handler.prototype.compare = function (msg, session, callback) {
    let zhajinhuaTableService = this.app.get("zhajinhuaTableService");
    callback(null, zhajinhuaTableService.compare(session.frontendId, session.id, msg.targetPlayerUid));
};

Handler.prototype.setAllIn = function (msg, session, callback) {
    let zhajinhuaTableService = this.app.get("zhajinhuaTableService");
    callback(null, zhajinhuaTableService.setAllIn(session.frontendId, session.id, msg.value));
};

Handler.prototype.notifyPlayers = function (msg, session, callback) {
    let zhajinhuaTableService = this.app.get("zhajinhuaTableService");
    callback(null, zhajinhuaTableService.sendChatMessage(session.frontendId, session.id, msg.message));
};
